package framework.eea.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import framework.eea.entity.Entity;

/**
 *
 * @author Markus Schr&ouml;der
 */
public class Graphics  {
    
    private java.awt.Graphics g;
    private JFrameGameContainer container;
    private Point translation;
    
    
    public Graphics(java.awt.Graphics g, JFrameGameContainer container) {
        this.g = g;
        this.container = container;
        translation = new Point();
    }
    
    public Graphics(JFrameGameContainer container) {
        this(null, container);
    }
    
    public void update(java.awt.Graphics g) {
        this.g = g;
    }
    
    
    
    
    public Dimension centerEntity(Entity e) {
        int w = g.getClipBounds().width;
        int h = g.getClipBounds().height;
        int deltaX = (w/2) - e.getPosition().x;
        int deltaY = (h/2) - e.getPosition().y;
        translate(deltaX, deltaY);
        return new Dimension(deltaX, deltaY);
    }
    
    public void resetTranslation() {
        translate(-translation.x, -translation.y);
    }
    
    public void translate(int x, int y) {
        g.translate(x, y);
        translation.x += x;
        translation.y += y;
    }
    
    public boolean contains(Rectangle rect) {
        Rectangle bounds = g.getClipBounds();
        boolean notContains = 
                (rect.x + translation.x) > bounds.width || 
                (rect.y + translation.y) > bounds.height ||
                (rect.x + translation.x) + rect.width < 0 ||
                (rect.y + translation.y) + rect.height < 0;
        return !notContains;
    }
    
    
    
    public Color getColor() {
        return g.getColor();
    }

    public void setColor(Color color) {
        g.setColor(color);
    }
    
    public Font getFont() {
        return g.getFont();
    }
    
    public int getFontHeight() {
        return g.getFontMetrics().getHeight();
    }
    
    
    
    
    public void drawString(String str, int x, int y) {
        g.drawString(str, x, y);
    }
    
    public void drawString(String str, int x, int y, int size) {
        Font f = new Font(g.getFont().getName(), g.getFont().getStyle(), size);
        Font old = g.getFont();
        g.setFont(f);
        
        g.drawString(str, x, y);
        
        g.setFont(old);
    }
    
    public void drawStringHoriCenter(String str, int centerX, int y) {
        int w = g.getFontMetrics().stringWidth(str);
        drawString(str, centerX - w/2, y);
    }
    
    public void drawStringCenter(String str, int centerX, int centerY) {
        int w = g.getFontMetrics().stringWidth(str);
        int h = getFontHeight();
        drawString(str, centerX - w/2, centerY + h/2);
    }
    
    public void drawStringHoriCenter(String str, int centerX, int y, int size) {
        Font f = new Font(g.getFont().getName(), g.getFont().getStyle(), size);
        Font old = g.getFont();
        g.setFont(f);
        
        int w = g.getFontMetrics().stringWidth(str);
        drawString(str, centerX - w/2, y);
        
        g.setFont(old);
    }
    
    
    public void drawLine(int x1, int y1, int x2, int y2) {
        g.drawLine(x1, y1, x2, y2);
    }
    
    
    
    public void clear(Color c) {
        Color before = g.getColor();
        g.setColor(c);
        fillRect(g.getClipBounds());
        g.setColor(before);
    }
    
    public void drawRect(Entity entity) {
        drawRect(entity.getRect());
    }
    
    public void drawRect(Rectangle rect) {
        g.drawRect(rect.x, rect.y, rect.width, rect.height);
    }
    
    
    
    public void fillRect(Rectangle rect) {
        fillRect(rect.x, rect.y, rect.width, rect.height);
    }
    
    public void fillRect(int x, int y, int w, int h) {
        g.fillRect(x,y,w,h);
    }
    
    
    
    
    public boolean drawImage(String ref) {
        return drawImage(ref, 0, 0);
    }
    
    public boolean drawImage(String ref, int x, int y) {
        return drawImage(container.getImage(ref), x, y);
    }
    
    public boolean drawImageCenterLocation(String ref, int centerX, int centerY) {
        Image img = container.getImage(ref);
        int w = img.getWidth(null);
        int h = img.getHeight(null);
        return drawImage(container.getImage(ref), centerX - w/2, centerY - h/2);
    }
    
    public boolean drawImage(String ref, Point position, Dimension size) {
        return drawImage(ref, new Rectangle(position, size));
    }
    
    public boolean drawImage(String ref, Entity entity) {
        return drawImage(ref, entity.getRect());
    }
    
    public boolean drawImage(String ref, Rectangle rect) {
        return drawImage(container.getImage(ref), rect.x, rect.y, rect.width, rect.height);
    }
    
    public boolean drawImage(String ref, Rectangle dest, Rectangle src) {
        BufferedImage img = (BufferedImage) container.getImage(ref);
        img = img.getSubimage(src.x, src.y, src.width, src.height);
        return drawImage(img, dest.x, dest.y, dest.width, dest.height);
    }
    
    public boolean drawImage(String ref, Entity e, Rectangle src) {
        int deltaImgH = src.height - e.getSize().height/2;
        return drawImage(ref,
                         new Rectangle(e.getPosition().x - src.width/2, e.getPosition().y - deltaImgH, src.width, src.height),
                         src);
    }
    
    private boolean drawImage(Image image, int x, int y, int w, int h) {
        //Rectangle r = g.getClipBounds();
        //if(x+w < 0 || y+h < 0 || x > r.width || y > r.height)
        //    return false;
        return g.drawImage(image, x, y, w, h, null);
    }
    
    private boolean drawImage(Image image, int x, int y) {
        return g.drawImage(image, x, y, null);
    }
    
    
}
